package me.chenqiang.tre.rule;

import java.util.Collection;

import java.util.List;

import me.chenqiang.tre.entity.Card;
import me.chenqiang.tre.entity.Device;
import me.chenqiang.tre.expression.Expression;

/**
 * 表示消费规则的类
 */
public class Rule {

	protected List<Expression> expressions;
	protected Action action;
	protected String name;

	/**
	 * 
	 * @param name        规则名称
	 * @param expressions 规则中的条件表达式，当全部条件都满足时匹配规则，即合取式
	 * @param action      匹配规则后的操作参数
	 */
	public Rule(String name, Collection<? extends Expression> expressions, Action action) {
		super();
		this.name = name;
		this.expressions = List.copyOf(expressions);
		this.action = action;
	}

	/**
	 * 根据发起交易的卡和终端判断是否匹配规则
	 * 
	 * @param card
	 * @param device
	 * @return
	 */
	public boolean matches(Card card, Device device) {
		for (Expression expr : expressions) {
			if (!expr.evaluate(card, device)) {
				return false;
			}
		}
		return true;
	}

	public Action getAction() {
		return this.action;
	}

	public List<Expression> getExpressions() {
		return expressions;
	}

	public String getName() {
		return name;
	}
}
